 package com.gmrz.uaf.protocol.v1.validaton.reg;
 
 import com.gmrz.uaf.common.Constants;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.uaf.protocol.v1.schema.ASMRegResponse;
import com.gmrz.uaf.protocol.v1.schema.AuthenticatorSpec;
import com.gmrz.uaf.protocol.v1.schema.RegistrationAssertion;
import com.gmrz.uaf.protocol.v1.validaton.Validator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
 
 public class RegAssertionValidator
   implements Validator<RegistrationAssertion>
 {
   private static final Logger LOG = LogManager.getLogger(RegAssertionValidator.class);
   private AuthenticatorSpec spec;
   private String fcpbase64;
 
   public RegAssertionValidator(AuthenticatorSpec spec, String fcpbase64)
   {
     this.spec = spec;
     this.fcpbase64 = fcpbase64;
   }
 
   public void validate(RegistrationAssertion assertion)
     throws RegAssertionValidationException
   {
     String regScheme = assertion.getKeyRegistrationDataScheme();
     if ((regScheme == null) || (!regScheme.equals(this.spec.getAssertionScheme()))) {
       LOG.error("Registration Scheme should be[" + this.spec.getAssertionScheme() + "] instead of[" + regScheme + "]");
       throw new RegAssertionValidationException(UAFErrorCode.PROTOCOL_ASSERTION_SCHEME_INVALID);
     }
 
     ASMRegResponse asm = assertion.getAuthenticatorResponse();
     if (asm == null) {
       LOG.error("Authenticator Registration Assertion is missing, rejecting the Reg response");
       throw new RegAssertionValidationException(UAFErrorCode.PROTOCOL_SIGNATURE_VALIDATION_FAILED);
     }
 
     Constants.AttestationType actual = asm.getAttestationType();
     List<Constants.AttestationType> allowedList = this.spec.getAttestationTypesAsList();
     if ((actual == null) || (!allowedList.contains(actual))) {
       LOG.error(actual.getName() + "] is not present in metadata for AAID[" + this.spec.getAAID() + "]");
       throw new RegAssertionValidationException(UAFErrorCode.PROTOCOL_ATTESTATION_TYPE_NOT_FOUND);
     }
 
     KeyRegDataValidator krdValidator = new KeyRegDataValidator(this.spec, this.fcpbase64);
     krdValidator.validate(asm.getKRD());
   }
 }
